<template>
  <div class="authentication">
    <div class="com-top-nav">
      <van-nav-bar
        title="添加教育背景"
        left-arrow
        @click-left="onClickLeft"
      />
    </div>
    <div>
      <van-cell-group>
        <p class="cell-group-tips">我的教育背景（必填）</p>
        <van-field
          v-model="formData.schoolName"
          required
          clearable
          label="学校"
          placeholder="请填写学校"
        />
        <zx-picker title="专业" :dataList="majorList" v-model="formData.major"></zx-picker>
        <zx-picker title="学历" :dataList="educationList" v-model="formData.education"></zx-picker>
        <p class="cell-group-tips">主修课程和荣誉奖励</p>
        <van-field
          type="textarea"
          v-model="formData.reward"
          required
          clearable
          :autosize="{maxHeight: '300', minHeight: '100'}"
          placeholder="请输入您的主修课程和荣誉奖励"
        />
      </van-cell-group>
      <div class="button">
        <van-button size="large" @click="handleSubmit">保存</van-button>
      </div>

    </div>
  </div>
</template>
<script>
  import validator from '../../../common/validatorFunction';
  import zxPicker from '../../../components/zx-picker'

  export default {
    components: {
      zxPicker
    },
    created(){
      this.init()
    },
    data(){
      return {
        isShowError: false,
        errorTips: '',
        majorList: [{values: ['计算机', '哲学', '经济学', '法学', '教育学', '文学', '历史学', '理学', '工学', '农学', '医学', '军事学', '管理学', '艺术学' ]}],
        educationList: [{values: ['小学', '中学', '高中', '专科', '本科', '硕士', '博士']}],
        formData: {
          schoolName: '',
          major: [],
          education: [],
          reward: ''
        },
//        rules: {
//          name: [
//            {require: true, message: '请输入姓名'}
//          ],
//          schoolName: [
//            {require: true, message: '请填写学校名称'}
//          ],
//          suggestion: [
//            {require: true, message: '请输入您的自我评价'}
//          ]
//        }
      }
    },
    mounted() {

    },
    methods: {
      onClickLeft() {
        this.$router.go(-1);
      },
      init(){
        let edId = this.$route.params.edId;
        if(edId >= 0){
          this.axios.get('/api/user/edu_bg/showById',{params:{edId}}).then(res=>{
            console.log(res.data.data);
              if(res.data.err_code === 0){
                this.formData.schoolName = res.data.data.school;
                this.formData.major[0] = res.data.data.speciality;
                this.formData.education[0] = res.data.data.academic;
                this.formData.reward = res.data.data.honor;
              }
          }).catch(e=>{
            this.$toast({message:e})
          })          
        }
        else{
          this.formData.major[0] = '计算机';
          this.formData.education[0] = '本科';
        }
      },
      handleSubmit() {
        // 保存数据
        const rulesResult = validator(this.formData, this.rules);
        console.log(rulesResult);
        if (rulesResult.result) {
          let speciality =this.formData.major[0];
          let academic=this.formData.education[0];
          let params = {
            edId: this.$route.params.edId,
            uid:JSON.parse(sessionStorage.getItem("user")).uid,
            school:this.formData.schoolName,
            academic:academic,
            speciality:speciality,
            honor:this.formData.reward,
          }
          if(!params.uid || !params.school || !params.speciality || !params.academic || !params.honor ){
          this.$toast({message:'请填写完整再提交', position: 'middle'});
            return;
          }
          this.axios.get('/api/user/edu_bg/create',{params}).then((r) => {
            if(r.data.err_code == 0){
              console.log(r.data)
              if(this.$route.params.edId >= 0){
                this.$toast({message:"修改成功！", position: 'middle'});  
              }
              else{
                this.$toast({message:"添加成功！", position: 'middle'});
              }
            }
          }).catch((err) => {
            this.$toast({message: err, position: 'middle'});
          });
        } else {
          const alertMessage = [];
          for (const key in rulesResult.detail) {
            if (!rulesResult.detail[key].isPass) {
              alertMessage.push(rulesResult.detail[key].message);
            }
          }
          this.isShowError = true;
          this.errorTips = alertMessage[0];
          this.$toast({message:this.errorTips, position: 'middle'});
        }
      }
    }
  }
</script>

<style scoped lang="less" type="text/less">
  @import url('../../../../static/style/var');
  .cell-group-tips {
    margin: 0;
    font-weight: 400;
    font-size: 14px;
    color: @weak-tips-color;
    padding: 10px 15px;
  }
  .button {
    padding-left: 10px;
    padding-right: 10px;
    margin-top: 30px;
  }


</style>
